package jaux.tank.generator.domain;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import jaux.tank.generator.domain.TankEntityField.FieldType;
import jaux.tank.generator.domain.TankEntityField.FieldValidation;
import jaux.tank.generator.util.StringUtil;
import lombok.Data;

@Data
public class TankEntity {
	
	public static final String DEFAULT_ENTITY_NAME = "NewEntity";
	
	public static final String ID_FIELD_NAME = "id";
	
	public enum DTO{
		MAPSTRUCT,NO
	}
	public enum Paginate{
		PAGINATION, INFINITE, NO
		
	}
	public enum ServiceType{
		NO, RESOURCE, SERVICE_CLASS, SERVICE_IMPL
	}

	
	private List<String> javaDocs;
	
	private List<String> annotations;
	
	private String name;
	
	private String id;
	
	private String tableName;
	
	private String comment;
	
	private List<TankEntityField> fields;
	
	private DTO dto;
	
	private Paginate paginate;
	
	private ServiceType serviceType;
	
	private boolean filtered;
	
	private boolean readOnly;
	
	private boolean skipClient;
	
	private boolean skipServer;
	
	private boolean auditEnabled;
	
	private boolean softDeleteEnabled;
	
	public TankEntity() {
		this(DEFAULT_ENTITY_NAME);
	}
	
	public TankEntity(String name) {
		this.name = name;
		this.id = UUID.randomUUID().toString();
		this.fields = new java.util.ArrayList<>();
		this.fields.add(getIdField());
		this.serviceType = ServiceType.SERVICE_IMPL;
		this.tableName = StringUtil.snakeCase(name);
		this.dto = DTO.NO;
		this.javaDocs = new ArrayList<>();
		this.annotations = new ArrayList<>();
		this.paginate = Paginate.PAGINATION;
	}
	
	private TankEntityField getIdField() {
		TankEntityField idField = new TankEntityField();
		idField.setName(ID_FIELD_NAME);
		idField.setType(FieldType.STRING);
		FieldValidation idValidation = new FieldValidation();
		idValidation.setRequired(true);
		idValidation.setUnique(true);
		idField.setValidation(idValidation );
		return idField;
	}
	
	public void setFields(List<TankEntityField> fields) {
		// temp fields
		this.fields = new ArrayList<>();
		if (fields == null) {
			this.fields.add(getIdField());
		}else {
			// add id field
			if(fields.stream().noneMatch(f->ID_FIELD_NAME.equals(f.getName()))) {
				this.fields.add(getIdField());
			}
			this.fields.addAll(fields);
		}
	}
}
